package cn.xhh.dailyreview.client.utils;

import java.util.HashMap;

import com.google.gwt.user.client.rpc.IsSerializable;

public class Result implements IsSerializable{
	
	public static enum Status {
		/** operation succeeded */
		SUCCESS,
		
		/** operation failed */
		FAILURE,
		
		/** server code error, details should have been logged */
		ERR_SERVER,
		
		/** some values exist in the system */
		VALUES_EXIST,
		
		/* three other cases: A, B and C */
		OTHER_A, OTHER_B, OTHER_C, 
	}
	
	/** The status of associated with this result */
	public Status status;
	/** Some values, if exists */
	private HashMap<String, String> values;
	
	public Result(Status status) {
		this.status = status;
	}
	
	/**
	 * New Result with status of <code>SUCCESS</code>.
	 * @see #success()
	 */
	public Result() {
		this(Status.SUCCESS);
	}

	/**
	 * @return <code>true</code> if status is <code>SUCCESS</code>
	 */
	public boolean successful() {
		return status == Status.SUCCESS;
	}
	
	/**
	 * @return <code>true</code> if status is not <code>SUCCESS</code>
	 */
	public boolean unsuccessful() {
		return status != Status.SUCCESS;
	}
	
	public void addValue(String key, String value) {
		if (values == null) values = new HashMap<String, String>();
		values.put(key, value);
	}
	
	public String getValue(String key) {
		return (values == null) ? null : values.get(key);
	}
	
	/**
	 * @return <code>true</code> if the result is not null 
	 * and its status is <code>SUCCESS</code>
	 */
	public static boolean isSuccessful(Result result) {
		return result!=null && result.successful();
	}
	
	/**
	 * @return <code>true</code> if the result is null 
	 * or its status is not <code>SUCCESS</code>
	 */
	public static boolean notSuccessful(Result result) {
		return !isSuccessful(result);
	}

	/**
	 * @return A new Result with status of <code>SUCCESS</code>.
	 * @see #Result()
	 */
	public static Result success() {
		return new Result(Status.SUCCESS);
	}
	
	/**
	 * @return A new Result with status of <code>FAILURE</code>.
	 */
	public static Result failure() {
		return new Result(Status.FAILURE);
	}
	
	/**
	 * @return A new Result with status of <code>VALUES_EXIST</code>.
	 */
	public static Result valuesExist() {
		return new Result(Status.VALUES_EXIST);
	}
	
	/**
	 * @return A new Result with status of <code>ERR_SERVER</code>.
	 */
	public static Result errServer() {
		return new Result(Status.ERR_SERVER);
	}
	
	/**
	 * @return A new Result with status of <code>OTHER_A</code>.
	 */
	public static Result otherA() {
		return new Result(Status.OTHER_A);
	}
	
	/**
	 * @return A new Result with status of <code>OTHER_B</code>.
	 */
	public static Result otherB() {
		return new Result(Status.OTHER_B);
	}
	
	/**
	 * @return A new Result with status of <code>OTHER_C</code>.
	 */
	public static Result otherC() {
		return new Result(Status.OTHER_C);
	}
	
}
